package main.SQL;

import main.MainApp;
import main.SQL.objects.SQLDocumentStructure;
import main.Trees.ProjectNode;
import org.jdesktop.swingx.treetable.AbstractTreeTableModel;

import java.text.DecimalFormat;

public class SQLProjectModel extends AbstractTreeTableModel {

    private ProjectNode root;
    private SQLDocumentStructure docStructure;

    public SQLProjectModel() {
        this.root = MainApp.curProject;
    }

    public void fill () {

        this.root = new FillSQLProjectModel().run();
        MainApp.docStructure = new SQLDocumentStructure();
    }

    //region nullHigh(ProjectNode node) - обнуляем индикацию привязки работ
    public void nullHigh(ProjectNode node) {

        for (ProjectNode task : MainApp.taskMap.values()) task.setHigh("");
    }
    //endregion

    @Override
    public String getColumnName( int column ) {

        switch( column ) {

//            case 0: return "<html><center><p>ID</p></center></html>";
//            case 1: return "<html><center>Наименование</center></html>";
//            case 2: return "<html><center>Ед изм</center></html>";
//            case 3: return "<html><center>Физ объем</center></html>";
//            case 4: return "<html><center>Марка РД</center></html>";
            case 0: return "ID";
            case 1: return "Наименование";
            case 2: return "Ед изм";
            case 3: return "Физ объем";
            case 4: return "Марка РД";
            case 5: return "Код";
            case 6: return "%";
            case 7: return "high";
            case 8: return "taskUse";
            default: return "Unknown";
        }
    }

    @Override
    public int getColumnCount() {
        return 9;
    }

    @Override
    public Object getValueAt( Object node, int column ) {

        ProjectNode treeNode = ( ProjectNode ) node;
        switch( column ) {

            case 0: return treeNode.getShortName();
            case 1: return treeNode.getName();
            case 2: return treeNode.getUomValue();
            case 3: return treeNode.getPvValue();
            case 4: return treeNode.getWDCode();
            case 5: return treeNode.getTaskCodeValue();
            case 6:
                if (treeNode.isTask()) {
                    DecimalFormat df = new DecimalFormat("#.##%");
                    return df.format(treeNode.getTaskCompletePct() != null ? treeNode.getTaskCompletePct() : 0.0);
                } else {
                    return null;
                }
            case 7: return treeNode.getHigh();
            case 8: return treeNode.getTaskUse();
            default: return "Unknown";        }
    }

    @Override
    public Object getChild(Object node, int index) {

        ProjectNode treeNode = ( ProjectNode ) node;
        return treeNode.getChildAt( index );

    }

    @Override
    public int getChildCount( Object parent ) {

        ProjectNode treeNode = ( ProjectNode ) parent;
        return treeNode.getChildCount();

    }

    @Override
    public int getIndexOfChild( Object parent, Object child ) {

        ProjectNode treeNode = ( ProjectNode )parent;

        for( int i=0; i>treeNode.getChildCount(); i++ ) {
            if( treeNode.getChildAt( i ) == child ) return i;
        }
        return 0;
    }

    @Override
    public Object getRoot() {
        return root;
    }

    public SQLDocumentStructure getDocStructure() {
        return docStructure;
    }

}
